#include "uart.h"
#include <stdio.h>



void uart_config()
{
    rcu_periph_clock_enable(RCU_GPIOA);

    gpio_mode_set(TX_PORT,GPIO_MODE_AF,GPIO_PUPD_NONE,TX_PIN);
    gpio_output_options_set(TX_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, TX_PIN);
    gpio_af_set(TX_PORT,GPIO_AF_7,TX_PIN);
    gpio_mode_set(RX_PORT,GPIO_MODE_AF,GPIO_PUPD_NONE,RX_PIN);
    gpio_af_set(RX_PORT,GPIO_AF_7,RX_PIN);

    rcu_periph_clock_enable(RCU_USART0);
    usart_deinit(USART0);
    usart_baudrate_set(USART0, 115200);
    usart_parity_config(USART0, USART_PM_NONE);
    usart_word_length_set(USART0, USART_WL_8BIT);
    usart_stop_bit_set(USART0, USART_STB_1BIT);
    usart_data_first_config(USART0, USART_MSBF_LSB);


    usart_receive_config(USART0,USART_RECEIVE_ENABLE);
    usart_transmit_config(USART0,USART_TRANSMIT_ENABLE);
    usart_interrupt_enable(USART0, USART_INT_RBNE);
    usart_interrupt_enable(USART0, USART_INT_IDLE);
    nvic_irq_enable(USART0_IRQn, 2, 0);

    usart_enable(USART0);
}

uint8_t buffer[1024] = {0};
uint8_t buff_index = 0;
void USART0_IRQHandler(void)
{
    if(usart_interrupt_flag_get(USART0,USART_INT_FLAG_RBNE) == SET)
    {
        usart_interrupt_flag_clear(USART0,USART_INT_FLAG_RBNE);
        buffer[buff_index] = usart_data_receive(USART0);
        buff_index++;
    }
    if(usart_interrupt_flag_get(USART0,USART_INT_FLAG_IDLE) == SET)
    {
        usart_data_receive(USART0);
        buffer[buff_index] = '\0';
        printf("recv:%slen:%d \n", buffer, buff_index);
        buff_index = 0;
    }
}



void uart2_config()
{
    rcu_periph_clock_enable(RCU_GPIOD);

    gpio_mode_set(TX2_PORT,GPIO_MODE_AF,GPIO_PUPD_NONE,TX2_PIN);
    gpio_output_options_set(TX2_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, TX2_PIN);
    gpio_af_set(TX2_PORT,GPIO_AF_7,TX2_PIN);
    gpio_mode_set(RX2_PORT,GPIO_MODE_AF,GPIO_PUPD_NONE,RX2_PIN);
    gpio_af_set(RX2_PORT,GPIO_AF_7,RX2_PIN);

    rcu_periph_clock_enable(RCU_USART2);
    usart_deinit(USART2);
    usart_baudrate_set(USART2, 9600);
    usart_parity_config(USART2, USART_PM_NONE);
    usart_word_length_set(USART2, USART_WL_8BIT);
    usart_stop_bit_set(USART2, USART_STB_1BIT);
    usart_data_first_config(USART2, USART_MSBF_LSB);


    usart_receive_config(USART2,USART_RECEIVE_ENABLE);
    usart_transmit_config(USART2,USART_TRANSMIT_ENABLE);
    usart_interrupt_enable(USART2, USART_INT_RBNE);
    usart_interrupt_enable(USART2, USART_INT_IDLE);
    nvic_irq_enable(USART2_IRQn, 6, 0);

    usart_enable(USART2);
}


void USART2_IRQHandler(void)
{
    if(usart_interrupt_flag_get(USART2,USART_INT_FLAG_RBNE) == SET)
    {
        usart_interrupt_flag_clear(USART2,USART_INT_FLAG_RBNE);
        buffer[buff_index] = usart_data_receive(USART2);
        buff_index++;
    }
    if(usart_interrupt_flag_get(USART2,USART_INT_FLAG_IDLE) == SET)
    {
        usart_data_receive(USART2);
        buffer[buff_index] = '\0';
        get_gps_data(buffer);
        buff_index = 0;
    }
}



void uart_recv(uint8_t * data, uint8_t len)
{
    printf("recv:%s len:%d \n", data, len);

}

void uart_send_byte(uint8_t data) {
    usart_data_transmit(USART0, data);
    while(0 == usart_flag_get(USART0, USART_FLAG_TBE));
}
int fputc(int ch, FILE *f) {
    uart_send_byte((uint8_t)ch);
    return ch;
}

